import React, { PropTypes } from 'react';
import { Router, Route, IndexRoute, Link } from 'react-router';
import App from '../pages/App';
import Login from '../pages/Login';
import OrderApply from '../pages/OrderApply';
import OrderCheckList from '../pages/OrderCheckList';
import OrderCheckOne from '../pages/OrderCheckOne';
import OrderHistory from '../pages/OrderHistory';
import StudentQuery from '../pages/StudentQuery';
import StudentImport from '../pages/StudentImport';
import ImportDetail from '../pages/Import/ImportDetail';
import OrderReapply from '../pages/OrderReapply'
import CompanyImport from '../pages/CompanyImport'
import CompanyQuery from '../pages/CompanyQuery'
import CompanyNotify from '../pages/CompanyNotify'
import StudentNotify from '../pages/StudentNotify'
import EnterpriseCreate from '../pages/EnterpriseCreate'
import Notification from '../pages/Notification'

import Home from '../components/others/Home'
import NotFound from '../components/others/NotFound'
import AuthManager from '../services/auth'

function requireAuth(nextState, replace) {
  if (!AuthManager.loggedIn()) {
    replace({
      pathname: '/login',
      state: {nextPathname: nextState.location.pathname}
    })
  }
}

const Routes = ({ history }) =>
  <Router history={history}>
    <Route path="/" component={App} onEnter={requireAuth}>
      <IndexRoute component={Home} onEnter={requireAuth}/>
      <Route path="/order/request" component={OrderApply} onEnter={requireAuth}/>
      <Route path="/order/check" component={OrderCheckList} onEnter={requireAuth}/>
      <Route path="/order/checkOne" component={OrderCheckOne} onEnter={requireAuth}/>
      <Route path="/order/reapply" component={OrderReapply} onEnter={requireAuth}/>
      <Route path="/order/timeline" component={OrderHistory} onEnter={requireAuth}/>
      <Route path="/student/query" component={StudentQuery} onEnter={requireAuth}/>
      <Route path="/student/import" component={StudentImport} onEnter={requireAuth}/>
      <Route path="/student/notify" component={StudentNotify} onEnter={requireAuth}/>
      <Route path="/company/import" component={CompanyImport} onEnter={requireAuth}/>
      <Route path="/company/query" component={CompanyQuery} onEnter={requireAuth}/>
      <Route path="/company/notify" component={CompanyNotify} onEnter={requireAuth}/>
      <Route path="/enterprise/create" component={EnterpriseCreate} onEnter={requireAuth}/>
      <Route path="/upload/detail" component={ImportDetail} onEnter={requireAuth}/>
      <Route path="/notification" component={Notification} onEnter={requireAuth}/>
      <Route path="/order/recheck" component={OrderCheckList} onEnter={requireAuth}/>

    </Route>
    <Router path="/login" component={Login} />
    <Router path="*" component={NotFound} />

  </Router>;

Routes.propTypes = {
  history: PropTypes.any,
};

export default Routes;
